package com.psdi.element;

import com.psdi.Main;
import com.psdi.manager.GameLoader;
import com.psdi.manager.GameManager;
import com.psdi.physics.Collider;
import com.psdi.physics.CollisionSystem;

import javax.swing.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 门类
 * 用于处理游戏中的传送门元素
 */
public class Door extends ElementObject implements Collider {
    private int num=0;
    private float animationTime;
    public Door(int x, int y, int width, int height, ImageIcon image) {
        super(x, y, width, height, image);
        tag=Tag.DOOR;
        CollisionSystem.INSTANCE.addElement(this);
    }

    public Door() { }

    @Override
    public Size colliderSize() {
        return this.transform.size;
    }

    @Override
    public Position colliderPosition() {
        return this.transform.position;
    }

    @Override
    public Tag colliderTag() {
        return this.tag;
    }

    @Override
    public Set<Tag> colliderFilterTag() {
        Set<Tag> set = new HashSet<>();
        set.add(Tag.BULLET);
        set.add(Tag.KNIFE);
        return set;
    }

    @Override
    public void onCollisionStay(ElementObject other) {
        if(other instanceof Player) {
            GameManager.level = "第二关";
            setAlive(false);
        }
    }

    @Override
    public void update() {
        num%=7;
        // 更新传送阵图片
        setImage(GameLoader.iconMap2.get(tag.toString()).get(num));
        animationTime -= Main.DELTA_TIME;
        if(animationTime <=0){
            animationTime = 0.2f;
            num++;
        }
    }

    public ElementObject CreateDoor(Position pos) { //传入敌人死亡时的位置信息
        this.transform = new Transform(pos,new Rotation(),new Size(80, 80));
        this.tag=Tag.DOOR;
        setImage(GameLoader.iconMap2.get(tag.toString()).get(0));
        CollisionSystem.INSTANCE.addElement(this);
        return this;
    }
}
